package awd.bj.jls.webs.controller;

import awd.bj.jls.webs.api.model.User;
import awd.bj.jls.webs.utils.DefaultQueryParam;
//import awd.bj.jls.webs.utils.ExcelUtil;
import awd.bj.jls.webs.utils.QueryParam;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BaseAction {
    /**
     * 获取当前登录用户信息
     * @return
     */
    protected User getCurrentUser(){
        User users = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return users;
    }

    protected Map<String,Object> getBaseModelData(){
        Map<String,Object> data = new HashMap<>(3);
        User user = getCurrentUser();
        data.put("user",user.getUsername());
        data.put("jsbh",user.getJsbh());
        return  data;
    }

    /**
     * 获取渲染页面路径
     * @param request
     * @return
     */
    protected String getRenderFilePath(HttpServletRequest request){
        String pagePath = request.getServletPath();
        pagePath = pagePath.substring(1).substring(0,pagePath.indexOf('.')-1);
        return pagePath;
    }

    /**
     * 获取查询参数，便于后期封装
     * @param request
     * @param params
     * @return
     */
    protected QueryParam getBaseQueryParam(HttpServletRequest request,Map<String,Object> params){
       QueryParam queryParam = new QueryParam();
       queryParam=DefaultQueryParam.requestUtil(request);
       return queryParam;
    }

//    @PostMapping("/exportExcel")
//    @ResponseBody
//    protected void exportExcel(String data,String title,HttpServletRequest request, HttpServletResponse response){
//        Map maps = (Map) JSON.parse(data);
//        Map<String,Object> headMap = (Map<String,Object>)maps.get("fieldname");
//        JSONArray jsonArray = new JSONArray((List<Object>)maps.get("rows"));
////        ExcelUtil.downloadExcelFile(title,headMap,jsonArray,response);
//    }
}
